const {
  GiftShopModel
} = require('../../../../utils/giftshop-model.js');
var giftShopModel = new GiftShopModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetail: {}, //订单详情
    isPassword: false, //支付密码弹出层
    timerName: '',
    day: '0',
    hou: '00',
    min: "00",
    sec: "00",
    orderAt: null, //创建订单时间
    shopId: '', //门店id
    orderCode: '', //订单号
    orderStatus: null, //1可支付 2超时
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      shopId: options.shopId, //门店id
      orderCode: options.orderCode, //订单号
    }, function () {
      that.getOrderDetail()
    })
  },



  /**
   * 订单详情
   */
  getOrderDetail() {
    let that = this;
    giftShopModel.userOrderDetail({
      merchantId: wx.getStorageSync('merchantId'), //商户ID
      shopId: that.data.shopId, //门店id
      orderCode: that.data.orderCode //订单号
    }).then(data => {
      that.setData({
        orderDetail: data,
        orderAt: data.orderAt || null,
        orderStatus: data.orderStatus
      }, () => {
        if (data.orderStatus != 3) {
          that.countDown()
        }
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 关闭支付页面
   */
  closePay: function (e) {
    this.setData({
      isPassword: false
    })
    this.getOrderDetail()
  },

  /**
   * 跳到首页
   */
  goHome: function () {
    wx.reLaunch({
      url: '/pages/giftShop/home/index'
    })
  },

  /**
   * 去支付
   */
  setPayPassword() {
    this.setData({
      shopId: this.data.shopId, //门店id
      orderCode: this.data.orderCode, //订单号
      isPassword: true
    })
  },

  /**
   * 
   * 支付成功
   */
  orderPaymentSuccess: function () {
    wx.redirectTo({
      url: '/pages/giftShop/payment/paymentSuccess/index?shopId=' + this.data.shopId + '&orderCode=' + this.data.orderCode
    })
  },

  // 倒计时
  countDown: function () {
    var that = this;
    var nowTime = new Date().getTime(); //现在时间（时间戳）
    var endTime = new Date(that.data.orderAt.replace(/-/g, '/')).getTime() + 0.5 * 60 * 60 * 1000; //结束时间（时间戳）
    var time = (endTime - nowTime) / 1000; //距离结束的毫秒数
    let day = parseInt(time / (60 * 60 * 24));
    let hou = parseInt(time % (60 * 60 * 24) / 3600);
    let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
    let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
    day = that.timeFormin(day);
    hou = that.timeFormin(hou);
    min = that.timeFormin(min);
    sec = that.timeFormin(sec);
    that.setData({
      day: that.timeFormat(day),
      hou: that.timeFormat(hou),
      min: that.timeFormat(min),
      sec: that.timeFormat(sec)
    })
    // 每1000ms刷新一次
    if (time > 0) {
      that.setData({
        timerName: setTimeout(function () { //这里把setInterval赋值给变量名为timer的变量
          that.countDown()
        }, 1000)
      })
    } else {
      that.setData({
        orderStatus: -1
      })
      clearInterval(that.data.timerName);
    }
  },
  //小于10的格式化函数（2变成02）
  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  },
  //小于0的格式化函数（不会出现负数）
  timeFormin(param) {
    return param < 0 ? 0 : param;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.closePay();
    clearInterval(this.data.timerName);
  },
})